<?php
/**
* Manage image sizes
*/

$images = new images();
$image_id = $variable->post('image_id', 'integer', 0);
$album_id = $images->album_id($image_id);

/**
* @todo Remove all the images first, if possible.
*/

# Remove thumbnails and force re-create them
if(isset($_POST['remove']))
foreach($_POST['remove'] as $size_id => $true)
{
	if($size_id == THUMBNAIL_ORIGINAL_SIZE_ID) continue;
	# remove thumbnail physically
	
	$thumbnail_file = realpath(FTPPATH_THUMBNAILS."/{$album_id}/{$size_id}/{$image_id}.jpg");
	if(is_file($thumbnail_file)) unlink($thumbnail_file);
	#echo "\r\n<br>Removing: {$thumbnail_file}";
}

# Modify the individual image's size details
if(isset($_POST['size']))
foreach($_POST['size'] as $size_id => $image)
{
	$size_id = (int)$size_id;
	$x = (int)$image['x'];
	$y = (int)$image['y'];
	$sizes_sql="
INSERT INTO gallery_images_sizes (
	image_id, size_id,
	cropnail_x, cropnail_y,
	subdomain_id, added_on, is_active, is_approved
) VALUES (
	{$image_id}, {$size_id},
	{$x}, {$y},
	{$subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()), 'Y', 'Y'
) ON DUPLICATE KEY UPDATE
	cropnail_x={$x},
	cropnail_y={$y},
	modified_ON=UNIX_TIMESTAMP(CURRENT_TIMESTAMP()),
	modified_counter=modified_counter+1
;";
	#echo "\r\n<br>", $sizes_sql;
	$db->query($sizes_sql);
	
	# Then, unlink all old images if available in the expected locations.
	$thumbnail_file = realpath(FTPPATH_THUMBNAILS."/{$album_id}/{$size_id}/{$image_id}.jpg");
	if(is_file($thumbnail_file)) unlink($thumbnail_file);
}

$messenger = new messenger('notice', 'Just fixed all those sizes.');
/*
Array
(
    [size] => Array
        (
            [3] => Array
                (
                    [x] => 0
                    [y] => 0
                )
        )
    [image_id] => 934
    [Submit] => Submit
)
*/

stopper::url(headers::back('images-list.php'));
?>